package com.cosina.game.crystallight.ui;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.util.Log;

import com.cosina.game.crystallight.ui.common.PaintSuite;

public class ResourceManager {
	
	private static Context context;
	
	public static void init(Context context){
		ResourceManager.context = context;
		PaintSuite.p4Text.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/TANGODI_.TTF"));
	}
	
	private static Map<String,Bitmap> cache =  new HashMap<String,Bitmap>();

	public static void closeQuietly(InputStream inputSteram){
		if(null == inputSteram)
			return;
		try{
			inputSteram.close();
		}catch(Exception ex){
		}
		return ;
	}
	
	public static Bitmap getNoCahce(String path){
		AssetManager am = context.getAssets();
		InputStream inputStream = null;
		try{
			inputStream = am.open(path);
			return BitmapFactory.decodeStream(inputStream);
		}catch(Exception ex){
			Log.e("cosina1985", ex.getMessage(), ex);
		}finally{
			closeQuietly(inputStream);
		}
		return cache.get(path);
	}
	
	public static Bitmap get(String path){
		if(cache.containsKey(path) == false){
			Bitmap result = getNoCahce(path);
			cache.put(path,result);
			return result;
		}
		return cache.get(path);
	}
}
